﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Util;
using Me.Amon.WMS.C;

namespace Me.Amon.OMS
{
    public partial class UpdateSku : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var cat = Request["cat"];
            if (!CharUtil.IsValidateHash(cat))
            {
                cat = "0";
            }

            var dba = new RDBEngine();
            ListCat(dba, cat);

            var spu = Request["spu"];
            if (CharUtil.IsValidateHash(spu))
            {
                dba.Reset();
                ReadSpu(dba, spu);
                return;
            }
            HdSpu.Value = "0";

            var sku = Request["sku"];
            if (CharUtil.IsValidateHash(sku))
            {
                dba.Reset();
                ReadSpu(dba, spu);
            }
        }

        #region 事件处理
        protected void TvCats_SelectedNodeChanged(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            //var dba = new DBAccess();
            //ListSpu(dba, node.Value);
        }

        protected void BtUpdateSku_Click(object sender, EventArgs e)
        {

        }

        protected void BtDeleteSku_Click(object sender, EventArgs e)
        {

        }

        protected void BtCancelSku_Click(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            Response.Redirect("~/OMS/Spu.aspx?cat=" + node.Value);
        }
        #endregion

        #region 目录
        private void ListCat(RDBEngine dba, string cat)
        {
            var root = new TreeNode();
            root.Value = "0";
            root.Text = "商品类别";
            TvCats.Nodes.Add(root);
            root.Selected = (cat == root.Value);

            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, "0");
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, cat);
            }
        }

        private void LoadCat(RDBEngine dba, TreeNode root, string cat)
        {
            dba.Reset();
            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, root.Value);
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);
                node.Selected = (cat == node.Value);

                LoadCat(dba, node, cat);
            }
        }
        #endregion

        private void ReadSpu(RDBEngine dba, string spu)
        {
            dba.AddTable(DSpu.TABLE);
            dba.AddWhere(DSpu.PID, spu);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                HdSpu.Value = "0";
                return;
            }

            DataRow row = dt.Rows[0];
            HdSpu.Value = spu;
            TbCode.Text = (string)row[DSpu.CODE];
            LlName.Text = (string)row[DSpu.NAME];
            LlUom.Text = (string)row[DSpu.UOM_ID];
            TbHypy.Text = (string)row[DSpu.HYPY];
            TbSpec.Text = (string)row[DSpu.SPEC];
            TbMode.Text = (string)row[DSpu.MODE];
            TbMaxInv.Text = "" + row[DSpu.MAX_INV];
            TbMinInv.Text = "" + row[DSpu.MIN_INV];
            TbVdY.Text = "" + row[DSpu.VALID_YEAR];
            TbVdM.Text = "" + row[DSpu.VALID_MONTH];
            TbVdd.Text = "" + row[DSpu.VALID_DAY];
        }

        private void ReadSku(RDBEngine dba, string sku)
        {
            dba.AddTable(DSpu.TABLE);
            dba.AddWhere(DSpu.ID, sku);
            var dt = dba.ExecuteSelect();
            if (dt.Rows.Count != 1)
            {
                return;
            }

            DataRow row = dt.Rows[0];
            HdId.Value = sku;
            TbCode.Text = (string)row[DSpu.CODE];
            LlName.Text = (string)row[DSpu.NAME];
            LlUom.Text = (string)row[DSpu.UOM_ID];
            TbHypy.Text = (string)row[DSpu.HYPY];
            TbSpec.Text = (string)row[DSpu.SPEC];
            TbMode.Text = (string)row[DSpu.MODE];
            TbMaxInv.Text = "" + row[DSpu.MAX_INV];
            TbMinInv.Text = "" + row[DSpu.MIN_INV];
            TbVdY.Text = "" + row[DSpu.VALID_YEAR];
            TbVdM.Text = "" + row[DSpu.VALID_MONTH];
            TbVdd.Text = "" + row[DSpu.VALID_DAY];
        }
    }
}